package com.example.lucky.draw.server.controller;

import com.example.lucky.draw.server.common.annotation.OperationLog;
import com.example.lucky.draw.server.common.result.JsonResult;
import com.example.lucky.draw.server.protocol.po.Activity;
import com.example.lucky.draw.server.protocol.po.Prize;
import com.example.lucky.draw.server.protocol.query.ActivityQuery;
import com.example.lucky.draw.server.service.IActivityService;
import io.swagger.annotations.ApiOperation;
import org.json.JSONException;
import org.springframework.batch.core.*;
import org.springframework.batch.core.explore.JobExplorer;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
import org.springframework.batch.core.repository.JobRestartException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
public class ActivityController {

    @Autowired
    private IActivityService activityService;

    @Autowired
    private JobLauncher jobLauncher;

    @Autowired
    private Job csvToDbJob;

    /**
     * jobExplorer 是Spring Batch提供的接口，用于访问和查询作业执行历史和元数据，获取所有作业，获取特定
     * 作业的所有执行
     */
    @Autowired
    private JobExplorer jobExplorer;

    @PostMapping("/draw/activity/getAll")
    public JsonResult<List<Activity>> findAll(@RequestBody ActivityQuery activityQuery) {
        List<Activity> activities = activityService.findAll(activityQuery);
        return JsonResult.ok(activities);
    }

    @OperationLog(operation = "新增抽奖活动")
    @PostMapping("/draw/activity/save")
    public JsonResult<String> save(@RequestBody Activity activity) {
        activityService.save(activity);
        return JsonResult.ok("新增成功");
    }

    @GetMapping("/activity/csvToDB")
    public JsonResult<String> csvToDB() throws JobInstanceAlreadyCompleteException, JobExecutionAlreadyRunningException, JobParametersInvalidException, JobRestartException {
        /**
         * 会利用JobExplorer来获取上次作业的参数
         * getNextJobParameters 会根据作业的历史执行情况，生成新得参数，以便继续执行作业
         * 可以确保每次作业执行都是唯一的
         */
        JobParameters jobParameters =
                new JobParametersBuilder(new JobParameters(), jobExplorer).getNextJobParameters(csvToDbJob).toJobParameters();
        JobExecution run = jobLauncher.run(csvToDbJob, jobParameters);
        return JsonResult.ok(run.getId().toString());
    }

    @PostMapping("/draw/activity/{id}")
    public JsonResult<Activity> findById(@PathVariable("id") Long id) {
        Activity activity=activityService.findActivityById(id);
        return JsonResult.ok(activity);
    }

    @PostMapping("/app/draw/activity/getAll")
    public JsonResult<List<Activity>> getAll(){
        return JsonResult.ok(activityService.findAll());
    }

    @ApiOperation("抽奖触发")
    @GetMapping("/app/draw/activity/trigger")
    public JsonResult<Prize> trigger(Long id) throws JSONException {
        return JsonResult.ok(activityService.trigger(id));
    }
}
